﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcCheckoutCheckin.CCoTcCheckoutCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.TcCommands.TcCheckoutCheckin.PrivateImplementationDetails;
using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.TcCommands.TcCheckoutCheckin;

public class CCoTcCheckoutCmd : CCoTcCheckoutCheckinBaseCmd
{
  public CCoTcCheckoutCmd() => this._enabler = new CApTcCheckoutCheckinEnabler();

  protected override void ShowSuccessMessage()
  {
    int num = (int) TxMessageBox.ShowModal(StringTable.TC_CHECKOUT_SUCCEDEED, base.Name, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }

  private string GetConfirmationString(TxObjectList selectedObjects, TxObjectList editableObjects)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxObject selectedObject in (Collection<ITxObject>) selectedObjects)
    {
      if (!((Collection<ITxObject>) editableObjects).Contains(selectedObject))
        ((Collection<ITxObject>) txObjectList).Add(selectedObject);
    }
    string checkoutConfirmation;
    if (((Collection<ITxObject>) txObjectList).Count < 11)
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.AppendLine(StringTable.TC_CHECKOUT_CONFIRMATION_INFO);
      foreach (ITxObject itxObject in (Collection<ITxObject>) txObjectList)
        stringBuilder.AppendLine("   " + itxObject.Name);
      stringBuilder.AppendLine(StringTable.TC_CHECKOUT_CONFIRMATION_QUESTION);
      checkoutConfirmation = stringBuilder.ToString();
    }
    else
      checkoutConfirmation = StringTable.TC_CHECKOUT_CONFIRMATION;
    return checkoutConfirmation;
  }

  private static TxObjectList FilterNewObjects(TxObjectList objects)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxObject itxObject in (Collection<ITxObject>) objects)
    {
      if (!TxTcUtilities.IsObjectOfNewPrototype(itxObject))
        ((Collection<ITxObject>) txObjectList).Add(itxObject);
    }
    return txObjectList;
  }

  private bool CanExecute(TxObjectList selectedObjects, TxObjectList editableObjects)
  {
    bool flag = ((Collection<ITxObject>) selectedObjects).Count == ((Collection<ITxObject>) editableObjects).Count;
    if (!flag)
    {
      if (((Collection<ITxObject>) editableObjects).Count == 0)
      {
        int num = (int) TxMessageBox.ShowModal(StringTable.TC_CHECKOUT_SELECTION_READONLY, base.Name, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
      }
      else
        flag = TxDoNotShowAgainMessageBox.Show(this.GetConfirmationString(selectedObjects, editableObjects), base.Name, "PsOnTCCheckoutConfirmation", MessageBoxButtons.YesNo, DialogResult.Yes) == DialogResult.Yes;
    }
    return flag;
  }

  protected override void ExecuteSpecificCommand(object cmdParams)
  {
    Cursor current = Cursor.Current;
    try
    {
      TxObjectList items1 = TxApplication.ActiveSelection.GetItems();
      TxObjectList editableObjects = CApTcCIOUtil.ExtractEditableObjects(CCoTcCheckoutCmd.FilterNewObjects(items1));
      if (!this.CanExecute(items1, editableObjects))
        return;
      bool? nullable = new CUiTcCheckoutDialog(!this._enabler.OnlyResourcesSelected).ShowDialog(true);
      if (!nullable.HasValue || !nullable.Value)
        return;
      Cursor.Current = Cursors.WaitCursor;
      TxErrorStack.Clear();
      bool flag = false;
      bool includeSubHierarchies = CUiTcCheckoutDialog.IncludeSubHierarchies;
      if (includeSubHierarchies && !this._enabler.OnlyResourcesSelected)
        flag = CUiTcCheckoutDialog.IncludeWorkareasAndResources;
      TxTcUtilities.CheckOut((ITxTcGlobalServicesProvider) (TxApplication.ActiveDocument.PlatformGlobalServicesProvider as TxTcGlobalServicesProvider), editableObjects, new TxTcCheckOutParams()
      {
        ChangeId = "",
        Reason = "",
        CheckOutRecursive = includeSubHierarchies,
        CheckOutProcess = true,
        CheckOutPlant = flag,
        CheckOutTools = flag
      });
      TxObjectList items2 = TxApplication.ActiveSelection.GetItems();
      TxApplication.ActiveSelection.Clear();
      TxApplication.ActiveSelection.SetItems(items2);
      Cursor.Current = current;
      this.ShowExecutionResult();
    }
    catch (Exception ex)
    {
      Cursor.Current = current;
      int num = (int) TxMessageBox.ShowModal(ex.Message, base.Name, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  public virtual string Name => StringTable.TC_CHECKOUT_CMD_NAME;

  public virtual string Bitmap => "TcCommands.TcCheckoutCheckin.Images.CheckOutInPSonTc.bmp";

  public virtual string LargeBitmap
  {
    get => "TcCommands.TcCheckoutCheckin.Images.CheckOutInPSonTcBig.png";
  }

  public virtual ITxImage Image => (ITxImage) TxImageManager.GetImageByKey("CheckOutTc");

  public virtual string Tooltip => StringTable.TC_CHECKOUT_CMD_TOOLTIP;

  public virtual string Description => StringTable.TC_CHECKOUT_CMD_DESCRIPTION;

  protected override string ErrWindowSubTitle => StringTable.TC_CHECKOUT_FAILED_SUBTITLE;

  protected override string ErrWindowInnerTitle => StringTable.TC_CHECKOUT_FAILED_OBJECTS;
}
